package com.qiahao.controller;

import com.qiahao.bean.User;
import com.qiahao.dto.ResponseData;
import com.qiahao.dto.UserDto;
import com.qiahao.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by Administrator on 2018/4/2 0002.
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    private IUserService userService;

    @Autowired
    public UserController(IUserService userService) {
        this.userService = userService;
    }

    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 密码
     * @return 操作结果
     * @throws AuthenticationException 错误信息
     */
    @PostMapping(value = "/login", params = {"username", "password"})
    public ResponseData getToken(String username, String password) {
        return userService.login(username, password);
    }

    /**
     * 用户注册
     *
     * @param user 用户信息
     * @return 操作结果
     * @throws AuthenticationException 错误信息
     */
    @PostMapping(value = "/register")
    public ResponseData register(User user) throws AuthenticationException {
        ResponseData data = userService.register(user);

        return data;
    }

    /**
     * 刷新密钥
     *
     * @param authorization 原密钥
     * @return 新密钥
     * @throws AuthenticationException 错误信息
     */
    @GetMapping(value = "/refreshToken")
    public String refreshToken(@RequestHeader String authorization) throws AuthenticationException {
        return userService.refreshToken(authorization);
    }


    /**
     * 修改密码
     *
     * @param user
     * @param newPassword
     * @return
     */
    @PostMapping(value = "/modifypswd")
    public ResponseData refreshToken(User user, String newPassword) {
        return userService.modifyPswd(user, newPassword);
    }

//    @GetMapping(value = "/list")
//    public String userList() {
//        return userService.refreshToken(authorization);
//    }
}
